﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Windows.Media.Animation;
using System.Windows.Media.Media3D;
using System.Collections;
using System.ComponentModel;
using System.Collections.Specialized;
using CrazyClient.Properties;


namespace CrazyClient
{
    /// <summary>
    /// Interaction logic for Viewer.xaml
    /// </summary>
    public partial class ViewerControl : UserControl
    {
        public ViewerControl()
        {
            InitializeComponent();
            if (Settings.Default.TestClient)
            {
                BorderBrush = Brushes.Red;
            }
            Viewport3D.IsHitTestVisible = false;
           
            //   this.IsHitTestVisible = false;
            _data = new CustomDictionary<string, object>();
            this.DataContext = this;
           
            _data.Add("SongOverride", 2);
        }

        public CustomDictionary<string, object> Data
        {
            get
            {
                return _data;
            }
            set
            {
                _data = value;
            }
        }
        bool _uses3D;
        public bool Uses3D
        {
            get
            {
                return _uses3D;
            }
            set
            {
                _uses3D = value;
                if (_uses3D)
                {
                    if (!MainGrid.Children.Contains(Viewport3D))
                        MainGrid.Children.Insert(0, Viewport3D);
                }
                else
                {
                    if (MainGrid.Children.Contains(Viewport3D))
                        MainGrid.Children.Remove(Viewport3D);
                }
            }
        }
        private CustomDictionary<string, object> _data;

        //public State CurrentState
        //{
        //    get
        //    {
        //        return _currentState;
        //    }
        //    set
        //    {
        //        _currentState = value;

        //        _currentState.OnSongChanged += new EventHandler(_currentState_OnSongChanged);
        //    }
        //}
        // CreateAllBase creator;
        //void _currentState_OnSongChanged(object sender, EventArgs e)
        //{
        // //   creator = (CreateAllBase)CurrentState.GetCreator(CurrentState.CurrentSong.CreatorApp);
        // //   creator.OnCreatorDone += new EventHandler(creator_OnCreatorDone);
        // //   creator.CurrentState=CurrentState;
        // //   creator.StartSong();


        //}

        //void creator_OnCreatorDone(object sender, EventArgs e)
        //{
        // //   CurrentState.StartNextSong();
        // ////   creator = (CreateAllBase)CurrentState.GetCreator(CurrentState.CurrentSong.CreatorApp);
        //  //  creator.OnCreatorDone += new EventHandler(creator_OnCreatorDone);
        // ////   creator.CurrentState = CurrentState;
        //  //  creator.StartSong();
        //}

        protected virtual void CompositionTarget_Rendering(object sender, EventArgs e)
        {

            
            Visual3DCollection mg = xamlContent.Children;
            
           
        }

        //  State _currentState;
    }


    public class CustomDictionary<TKey, TValue> : ICustomTypeDescriptor, INotifyPropertyChanged, INotifyCollectionChanged, IDictionary<TKey, TValue>
    {
        private Dictionary<TKey, TValue> _dic = new Dictionary<TKey, TValue>();
        public event PropertyChangedEventHandler PropertyChanged;
        public event NotifyCollectionChangedEventHandler CollectionChanged;
        PropertyDescriptorCollection m_PropertyDescriptorCollectionCache = new PropertyDescriptorCollection(null);



        private void OnCollectionChanged(object sender, NotifyCollectionChangedAction action, object value)
        {
            if (CollectionChanged != null)
            {
                CollectionChanged(sender, new NotifyCollectionChangedEventArgs(action, value));
            }
        }

        private void OnPropertyChanged(object sender, string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }


        #region IDictionary<TKey,TValue> Members

        public void Add(TKey key, TValue value)
        {
            _dic.Add(key, value);
            m_PropertyDescriptorCollectionCache.Add(new MyCustomClassPropertyDescriptor(key.ToString()));
               
            
            OnCollectionChanged(this, NotifyCollectionChangedAction.Add, new KeyValuePair<TKey, TValue>(key, value));

            OnPropertyChanged(this, "Values");
            OnPropertyChanged(this, "Keys");
            OnPropertyChanged(this, "Count");
        }



        #region ICustomTypeDescriptor Members

        public AttributeCollection GetAttributes()
        {
            throw new NotImplementedException();
        }

        public string GetClassName()
        {
            throw new NotImplementedException();
        }

        public string GetComponentName()
        {
            throw new NotImplementedException();
        }

        public TypeConverter GetConverter()
        {
            throw new NotImplementedException();
        }

        public EventDescriptor GetDefaultEvent()
        {
            throw new NotImplementedException();
        }

        public PropertyDescriptor GetDefaultProperty()
        {
            throw new NotImplementedException();
        }

        public object GetEditor(Type editorBaseType)
        {
            throw new NotImplementedException();
        }

        public EventDescriptorCollection GetEvents(Attribute[] attributes)
        {
            return new EventDescriptorCollection(null);
        }

        public EventDescriptorCollection GetEvents()
        {
            return new EventDescriptorCollection(null);
        }

        public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
           
           
            return m_PropertyDescriptorCollectionCache;
        }

        public PropertyDescriptorCollection GetProperties()
        {
            return ((ICustomTypeDescriptor)this).GetProperties(null);
        }

        public object GetPropertyOwner(PropertyDescriptor pd)
        {
            return this;
        }
       

        #region IDictionary<TKey,TValue> Members


        public bool ContainsKey(TKey key)
        {
            return _dic.Keys.Contains(key);
                
//            throw new NotImplementedException();
        }

        public ICollection<TKey> Keys
        {
            get { return _dic.Keys; }
        }

        public bool Remove(TKey key)
        {
            throw new NotImplementedException();
        }

        public bool TryGetValue(TKey key, out TValue value)
        {
            throw new NotImplementedException();
        }

        public ICollection<TValue> Values
        {
            get { throw new NotImplementedException(); }
        }

        public TValue this[TKey key]
        {
            get
            {
                return _dic[key];
            }
            set
            {
                _dic[key] = value;
                OnPropertyChanged(this, key.ToString());
            }
        }

        #endregion

        #region ICollection<KeyValuePair<TKey,TValue>> Members

        public void Add(KeyValuePair<TKey, TValue> item)
        {
            throw new NotImplementedException();
        }

        public void Clear()
        {
            _dic.Clear();   
        }

        public bool Contains(KeyValuePair<TKey, TValue> item)
        {
            throw new NotImplementedException();
        }

        public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }

        public int Count
        {
            get { throw new NotImplementedException(); }
        }

        public bool IsReadOnly
        {
            get { throw new NotImplementedException(); }
        }

        public bool Remove(KeyValuePair<TKey, TValue> item)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IEnumerable<KeyValuePair<TKey,TValue>> Members

        public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
           return _dic.GetEnumerator();
        }

        #endregion
    }

    public class MyCustomClassPropertyDescriptor : PropertyDescriptor
    {
        public MyCustomClassPropertyDescriptor(string key)
            : base(key.ToString(), null)
        {
        }
        public override bool CanResetValue(object component)
        { return true; }
        public override Type ComponentType
        { get { return typeof(Dictionary<string, Double>); } }
        public override object GetValue(object component)
        { return ((CustomDictionary<string,object>)component)[base.Name]; }
        public override bool IsReadOnly
        { get { return false; } }
        public override Type PropertyType
        {
            get
            {
                return typeof(string);
            }
            //set
          //  {
                //  String.IsNullOrEmpty(string value);
          //  }
        }
        public override void ResetValue(object component)
        { ((Dictionary<string, string>)component)[base.Name] = string.Empty; }
        public override void SetValue(object component, object value)
        {
            //((CustomDictionary<string, Double>)component)[base.Name] = value.();
        }
        public override bool ShouldSerializeValue(object component)
        { return false; }
    }
}

        #endregion
        #endregion